home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / fincalc.exe / FINCALC.CPP < prev    next >
C/C++ Source or Header  |  1991-10-09  |  11KB  |  347 lines

  1. //Eddy Vasile, PO Box 71313, LA CA 90071 or 70451,3333@COMPUSERVE
  2. //A simple program to illustrate financial concepts, classes
  3. //and data entry and display using C++
  4. //
  5. #include <math.h>         //need this for power, log and exp
  6. #include <dos.h>
  7. #include <stdio.h>
  8. #include <conio.h>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. //financial events are classes
  12. class Financial {
  13.     double PV;         //present value
  14.     double FV;         //future value      these guys are accessed only inside
  15.     double IntRate;    //interest rate
  16.     double NPer;       //number of periods
  17. public:
  18.     //construct class by initializing with parameter values
  19.     //the member functions calculate and return their own values
  20.     //if value is -1 then it needs to be calcualted, else it's given
  21.     Financial(double InPV, double InFV, double InIntRate, double InNPer) {
  22.                 PV=InPV; FV=InFV; IntRate=InIntRate; NPer=InNPer;}
  23.     double GetPV() {if (PV==-1) PV=FV/pow(1+IntRate,NPer); return PV;}
  24.     double GetFV() {if (FV==-1) FV=PV*pow(1+IntRate,NPer); return FV;}
  25.     double GetNPer() {if (NPer==-1) NPer=(log(FV)-log(PV))/log(1+IntRate);return NPer;}
  26.     double GetIntRate() {if (IntRate==-1)  IntRate=exp((log(FV)-log(PV))/NPer)-1;return IntRate;}
  27. };
  28. //function to open a window and a box around it
  29. //parameters are dimensions, border, fore and background colors
  30. void BoxWin(int x1,int y1,int x2,int y2,int BD,int FG,int BG)
  31. {
  32.     int i,TB,SID,TLC,TRC,BLC,BRC;
  33.     window(1,1,80,25);
  34.     textcolor(FG) ;
  35.     textbackground(BG);
  36.     switch (BD) {
  37.         case 0:;                          //No border option}
  38.         case 1:
  39.             TB  = 196;                     //Top Border
  40.             SID = 179;                     //Side Border
  41.             TLC = 218;                     //Top Left Corner
  42.             TRC = 191;                     //Top Right Corner
  43.             BLC = 192;                     //Bottom Left Corner
  44.             BRC = 217;                     //Bottom Right Corner
  45.             break;
  46.         case 2:
  47.             TB  = 205;
  48.             SID = 186;
  49.             TLC = 201; TRC = 187;
  50.             BLC = 200; BRC = 188;
  51.             break;
  52.         default:
  53.             BD  = 3;
  54.             TB  = 205;
  55.             SID = 179;
  56.             TLC = 213; TRC = 184;
  57.             BLC = 212; BRC = 190;
  58.             break;
  59.     }
  60.  
  61.     if(BD > 0) {                                // border?
  62.   // Top
  63.       gotoxy(x1,y1);                    // Window Origin
  64.       putch(TLC);                           // Top Left Corner
  65.       for (i=x1+1; i<x2; i++)                // Top Bar
  66.           putch(TB);
  67.       putch(TRC);                           // Top Right Corner
  68.  
  69.   // Sides
  70.       for (i=y1+1; i<y2; i++) {
  71.             gotoxy(x1,i);                 // Left Side Bar
  72.             putch(SID);
  73.             gotoxy(x2,i) ;                // Right Side Bar
  74.             putch(SID);
  75.       }
  76.   // Bottom
  77.       gotoxy(x1,y2);                       // Bottom Left Corner
  78.       putch(BLC);
  79.       for (i=x1+1; i<x2; i++)               // Bottom Bar
  80.           putch(TB);
  81.       putch(BRC);                         // Bottom Right Corner
  82.     }
  83.     window(x1+1,y1+1,x2-1,y2-1);
  84.     clrscr();
  85.     textcolor(FG) ;
  86.     textbackground(BG);
  87. }
  88. //sound a deep throated burp-beep
  89. void beep()
  90. {
  91.     sound(50);
  92.     delay(200);
  93.     nosound();
  94. }
  95. //create a string made out of same chars
  96. char *repeatstr(int c, int l)
  97. {
  98.     char s[80];
  99.     int i;
  100.     if (l>=80) l=79;
  101.     for (i=0;i<l;i++) s[i]=c;
  102.     s[l]='\0';
  103.     return(s);
  104. }
  105. //create, edit and grab a string from somewhere on the screen
  106. //**warning, use absolute window locations 'cause gettext doesn't respect windows
  107. int inputstr(char s[80],char *goodchars,int l, int x, int y)
  108. {
  109.     int i,j,p=0,tc=0,sl=strlen(s),Blob=177;
  110.     char ch;
  111.     char buffer[4096];
  112.     if (l>=80) l=79;
  113.     gotoxy(x,y);
  114.     printf("%s%s",s,repeatstr(Blob,l-sl));
  115.     do {
  116.         gotoxy(x+p,y);
  117.         ch=getch();
  118.         if (ch==0) {
  119.              ch=getch();
  120.              switch (ch) {
  121.                 case 15  : tc=-1;break;     //back tab
  122.                 case 'I' : tc=-1;break;     //page up
  123.                 case 'Q' : tc= 1;break;     //page down
  124.                 case 'H' : tc=-1;break;     //up
  125.                 case 'P' : tc= 1;break;     //down
  126.                 case 'K' : if (p>0) {        //left
  127.                                     p--;
  128.                                     gotoxy(x+p,y);
  129.                               }
  130.                               else beep();
  131.                               break;
  132.                 case 'M' : if (p<sl) {       //right
  133.                                     p++;
  134.                                     gotoxy(x+p,y);
  135.                               }
  136.                               else beep();
  137.                               break;
  138.                 case 'G' : if (sl*p!=0) {     //home
  139.                                     p = 0;
  140.                                     gotoxy(x,y);
  141.                               }
  142.                               else beep();
  143.                               break;
  144.                 case 'O' : if (sl>0 && p<sl) {//end
  145.                                     p=sl-1;
  146.                                     gotoxy(x+p,y);
  147.                               }
  148.                               else beep();
  149.                               break;
  150.                 case 'R' : beep();break;         //insert - screw it, no insert, too much work
  151.                 case 'S' : if (p<sl) {            //delete
  152.                                     sl--;
  153.                                     for (i=p+1;i<=sl;i++) putch(s[i]);
  154.                                     putch(Blob);
  155.                                     gotoxy(x+p,y);
  156.                               }
  157.                               break;
  158.                 default  : beep();
  159.              }
  160.         }
  161.         else {
  162.             if (ch>=' ' && ch<='~') {
  163.                 if (strchr(goodchars,ch) && p<l) {
  164.                     putch(ch);
  165.                     p++;
  166.                     sl++;
  167.                     gotoxy(x+p,y);
  168.                 }
  169.                 else beep();
  170.             }
  171.             if (ch==8) { //back space
  172.                 if (p*sl==0) beep();
  173.                 else {
  174.                     sl--;
  175.                     p--;
  176.                     gotoxy(x+p,y);
  177.                     for (i=p+1;i<=sl;i++) putch(s[i]);
  178.                     putch(Blob);
  179.                 }
  180.             }
  181.             if (ch==9 || ch==13) tc=1; //tab or return
  182.             if (ch==27) tc=2; //exit input completely
  183.         }
  184.         gettext(x,y,x+l,y,buffer);
  185.         i=0;j=0;
  186.         strcpy(s,"\0");
  187.         while (i<=l && buffer[j]!=Blob) {
  188.             if (strchr(goodchars,buffer[j])) s[i++]=buffer[j];
  189.             j +=2;
  190.         }
  191.         s[i]='\0';
  192.     } while (tc==0);  //tc=1 means go to next item, -1 prior, 2= done
  193.     return tc;
  194. }
  195. //function to display all members formatted
  196. void PrintAll(Financial F) {
  197.     BoxWin(20,15,65,22,2,WHITE,GREEN);
  198.     gotoxy(1,1);cputs("Present Value: ");
  199.     gotoxy(1,2);cputs("Future Value : ");
  200.     gotoxy(1,3);cputs("Periods      : ");
  201.     gotoxy(1,4);cputs("Interest Rate: ");
  202.  
  203.     gotoxy(17,1);printf("%11.2f",F.GetPV());
  204.     gotoxy(17,2);printf("%11.2f",F.GetFV());
  205.     gotoxy(17,3);printf("%11.2f",F.GetNPer());
  206.     gotoxy(17,4);printf("%11.2f",F.GetIntRate()*100);
  207.  
  208.     gotoxy(1,5);cputs("Press any key.."); getch();
  209. }
  210.  
  211.  
  212. //give some help
  213. void HelpMsg()
  214. {
  215.     window(1,1,80,25);
  216.     clrscr();
  217.     BoxWin(1,8,80,20,1,RED,CYAN);
  218. cputs("This simple C++ program illustrates object oriented techniques for finance.  \r\n");
  219. cputs("Financial events are CLASSES with 4 members: Present Value, Future Value,    \r\n");
  220. cputs("Interest Rate and Number of Periods. The Present Value PV is the $ amount    \r\n");
  221. cputs("deposited at Interest Rate R in order to obtain the Future Value FV after the\r\n");
  222. cputs("Number of Periods N. In order to calculate any member, the other 3 must be   \r\n");
  223. cputs("known in order to satisfy the formula FV-PV(1+R)^N=0. Choose the calculation \r\n");
  224. cputs("you want and enter the requested values. Example: choose the Future Value    \r\n");
  225. cputs("Calculation. You will be asked to enter PV R and N.  Enter 5000 6.75 3 to    \r\n");
  226. cputs("calculate the Future Value of a $5,000 deposit @6.75% for 3 years. The result\r\n");
  227. cputs("is $6,082.38. Press any key to continue..                                    \r\n");getch();
  228.  
  229.     BoxWin(1,1,80,11,2,YELLOW,BLUE);
  230.  
  231. cputs("To compound interest monthly, enter N as 36 (3 X 12) and R as 0.563          \r\n");
  232. cputs("(monthly interest = 6.75/12). FV is now $6,119.92. This is useful if you     \r\n");
  233. cputs("want to calculate the YIELD of a CD. Now that you know the monthly compounded\r\n");
  234. cputs("FV, choose the Interest Calculation and enter 5000 6119.92 3. The YIELD is   \r\n");
  235. cputs("6.97%. Modify the source code to compound weekly or daily. If you like this, \r\n");
  236. cputs("write a note to Eddy Vasile, PO Box 71313, LA, CA 90071 or 70451,3333 at     \r\n");
  237. cputs("CompuServe and I'll send you the code for Net Present Value and Internal Rate\r\n");
  238. cputs("of Return calculations with more elaborate functions. Press any key..        \r\n");
  239.     getch();
  240.     window(1,1,80,25);
  241.     clrscr();
  242. }
  243. //put prompts on the screen and stay in loop grabbing data 'till escape
  244. void getdata(char string1[], char string2[], char string3[],
  245.                  double *d1, double *d2, double *d3)
  246. {
  247.     char s1[80],s2[80],s3[80];
  248.     char *digits="0123456789.";
  249.     int rc=0,where=0;
  250.     strcpy(s1,"\0");strcpy(s2,"\0");strcpy(s3,"\0"); *d1= *d2= *d3= 0;
  251.     window(1,1,80,25);
  252.     clrscr();
  253.     BoxWin(1,1,70,5,2,YELLOW,BLUE);      //just for looks
  254.     cputs("Complete EVERY field, use Arrows, Tabs, Pg